<?php
namespace Api\Controller;
use Common\Model\GameModel;
use Common\Model\MerchPayModel;
use Common\Model\OrderModel;
use Common\Model\RedpackModel;
use Common\Model\RoomModel;
use Common\Model\UserModel;
use Common\Model\UtilsModel;
use Common\Model\WeixinModel;
use Common\Model\WeixinRedPackModel;
use Think\Controller;
class GameController extends Controller {
    const SUCCESS   =0;//成功
    const FAILURE   =1;//失败
    public function index(){
        $this->show('公共支持','utf-8');
    }

    /**
     * 获取房间列表,及介绍详情
     */
    public function getRoomList(){
        $roomList=RoomModel::getRoomInstance()->where('status='.RoomModel::ROOM_ONLINE)->select();
        $this->ajaxReturn(array(
            'status'=>0,
            'data'=>$roomList
        ));
    }
    public function getRoomPriceStatus(){
        $rid=I('post.rid');
        $startDate=date('Y-m-d');
        $endDate=date('Y-m-d',strtotime("+30 day"));

        $roomStatus     = M('r_status',null,DB_MAIN_CFG)->where('rid='.$rid." and date between '{$startDate}' and '{$endDate}' ")->getField('date,status',true);
        $specialPrice   = M('r_price_special',null,DB_MAIN_CFG)->where('rid='.$rid." and date between '{$startDate}' and '{$endDate}' ")->getField('date,price',true);
        $priceSet       = M('r_price_common',null,DB_MAIN_CFG)->where('rid='.$rid)->find();
        $this->ajaxReturn(
            array('status'=>0,
                'today'=>$startDate,
                'roomStatus'=>$roomStatus,
                'special'=>$specialPrice,
                'set'=>$priceSet)
        );
    }
    public function getRoomDetail(){
        $rid=I('post.rid');
        $room=RoomModel::getRoomInstance()->where('rid='.$rid.' and status='.RoomModel::ROOM_ONLINE)->find();
        if($room){
            $date=date('Y-m-d');
            $price=OrderModel::calcRoomFee($rid,$date);
            if($price['status']!=0){
                $room['price']=-1;
            }else{
                $room['price']=$price['fee'];
            }

            $bedKv=RoomModel::getBedKv();
            $room['bed']=$bedKv[$room['bed']];

            $this->ajaxReturn(array(
                'status'=>0,
                'data'=>$room
            ));
        }else{
            $this->ajaxReturn(array(
                'status'=>1,
                'msg'=>'房间已经下架'
            ));
        }
    }


    /**
     * 用户进入
     */
    public function userLogin(){
        $openId     = I('post.openid');
        $headPic    = I('post.headPic');
        $sex        = I('post.gender');
        $nickname   = I('post.nickname');
        $this->ajaxReturn(UserModel::login($openId,$nickname,$headPic,$sex));
    }

    


    public function buildOrder(){
        $rid=I('post.rid',0,'intval');
        $selectedDate=I('post.selectedDate');

        $linkman=I('post.linkman');
        $phone=I('post.phone');
        $checkInTime=I('post.checkInTime');
        $mem=I('post.mem');
        $totalMoney=I('post.totalMoney');
        
        $feeRet=OrderModel::calcRoomFee($rid,$selectedDate);
        if($feeRet['status']!=0){
            $this->ajaxReturn($feeRet);
        }

        if($totalMoney != $feeRet['fee']){
            $this->ajaxReturn(array('status'=>1,'msg'=>'房间价格有变动，请重新选择'));
        }
        $now=time();

        $uid=session('uid')+0;
        OrderModel::getOrderInstance()->startTrans();
        $oid=OrderModel::getOrderInstance()->add(
            array(
                'rid'=>$rid,
                'dayPrice'=>json_encode($feeRet['detail']),
                'uid'=>$uid,
                'linkman'=>$linkman,
                'phone'=>$phone,
                'checkInTime'=>$checkInTime,
                'mem'=>$mem,
                'totalMoney'=>$totalMoney,
                'status'=>OrderModel::ORDER_STATUS_UNPAY,
                'addTime'=>$now
            )
        );

        if($oid===false){
            $this->ajaxReturn(array('status'=>1,'msg'=>'服务器出现错误，请过段时间再试'));
        }
        
        $dateArr=explode(',',$selectedDate);
        $list=array();
        foreach($dateArr as $date){
            $dateItem=array(
                'rid'=>$rid,
                'date'=>$date,
                'status'=>RoomModel::ROOM_STATUS_WAIT_PAY,
                'oid'=>$oid,
                'addTime'=>$now
            );
            $list[]=$dateItem;
        }
        
        RoomModel::getStatusInstance()->addAll($list,null,true);
        OrderModel::getOrderInstance()->commit();

        $this->ajaxReturn(array('status'=>0,'msg'=>'success','oid'=>$oid,'totalMoney'=>$totalMoney));


    }



    public function getOrderList(){
        $uid=session('uid')+0;
        $oneYear=UtilsModel::ONE_DAY * 365;
        $list=OrderModel::getOrderInstance()->where('uid ='.$uid.' and addTime >'.$oneYear)
            ->order('oid desc')->limit(30)
            ->field('oid,rid,status,FROM_UNIXTIME(addTime,\'%m-%d\')as date')
            ->select();
        $roomKv=RoomModel::getRoomKv();
        $statusKv=OrderModel::getStatusKv();
        foreach($list as $k=>$v){
            $list[$k]['roomName']=$roomKv[$v['rid']];
            $list[$k]['statusName']=$statusKv[$v['status']];
        }
        $this->ajaxReturn($list);

    }




    public function getWXopenid(){
        $jscode=I('get.js_code');
        //https://api.weixin.qq.com/sns/jscode2session?appid=wx0bc6c5ceddbae730&secret=e774e7f275cca946bcd85e217cd2ebd8&grant_type=authorization_code&js_code=081w1PiU13K1pV0UuWlU1EQOiU1w1PiG
        $data=array(
            'appid'=>XIAO_APPID,
            'secret'=>XIAO_SECRET,
            'grant_type'=>'authorization_code',
            'js_code'=>$jscode
        );
        $return=UtilsModel::curl_request('https://api.weixin.qq.com/sns/jscode2session?','get',$data);
        if($return['status']==0){
            $return['data']=json_decode($return['data'],1);
        }
        $this->ajaxReturn($return);
    }
    
}